<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------

namespace app\common\logic;

use OSS\OssClient;
use OSS\Core\OssException;

class AuthLogic
{

    /**
     * User: Simony
     * Function:验证功能使用
     * @param $member_id
     * @param bool $verfify_real
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function isUseAuth($member_id, $verfify_real = false)
    {
        if ($verfify_real == false) return true;

        $memberModel = model('common/member/member');
        $member = $memberModel->getInfoByID($member_id);

        if (empty($member)) return ds_arr(ERROR, '会员不存在');

        $levelModel = model('common/member/level');
        $level_use_auth = moduleConfig("level_use_auth");

        if ($verfify_real) { //验证实名项
            $config = $level_use_auth[$levelModel::REAL_VERIFY];
            //无配置时默认让其等于不需要实名认证
            $real_verify_config = $config[$member['sex']][$member['level']] ?? 0;

            //当前是否真人
            $current_real_verify = $member['real_verify'];
            if ($current_real_verify < $real_verify_config) return ds_arr(JUMP_AUTH, '使用需请先实名认证');
        }
        //继续加其他验证代码

        return true;
    }

    /**
     * User: Simony
     * Function:
     * @param $member_id
     * @param $type 1动态 2项目
     * @return array
     */
    public function getDayReleaseContentNum($member_id, $type)
    {
        $memberModel = model('common/member/member');
        $member = $memberModel->getInfoByID($member_id);

        if (empty($member)) return ds_arr(ERROR, '会员不存在');


        //读取配置动态数
        $levelModel = model('common/member/Level');
        $level_auth = moduleConfig('level_auth');

        $config_name = $type == 1 ? $levelModel::DAY_RELEASE_DYNAMIC_NUM : $levelModel::DAY_RELEASE_PROGRAM_NUM;
        $sex = $member['sex'];
        $level = $member['level'];

        $day_release_num = $level_auth[$config_name][$sex][$level] ?? 0;
        return $day_release_num;
    }


    /**
     * User: Simony
     * Function: 今日剩余查看次数
     * @param $member_id
     * @param $type 1，会员信息 2微信+聊天社交 3，相册
     * @return array|int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function getDaySurplusViewNum($member_id, $config_name)
    {

        $levelModel = model('common/member/Level');
        switch ($config_name) {
            case $levelModel::VIEW_MEMBER_NUM:
                $type = 1; //1，会员信息
                break;
            case $levelModel::DAY_VIEW_MEMBER_SOCIAL_ACCOUNT_NUM:
                $type = 2; //2微信+聊天社交
                break;
            case $levelModel::DAY_VIEW_MEMBER_ALBUM_NUM:
                $type = 3; //3，相册
                break;
            default:
                return ds_arr(ERROR, '类型必须是：1，会员信息 2微信+聊天社交 3，相册');
                break;
        }

        $memberModel = model('common/member/Member');

        $member = $memberModel->getInfoByID($member_id);

        if (empty($member)) return ds_arr(ERROR, '会员不存在');

        //获取配置数
        $level_auth = moduleConfig('level_auth');
        $sex = $member['sex'];
        $level = $member['level'];
        //读取配置动态数 level_auth[$config_name][性别即1][等级]
        $config_num = $level_auth[$config_name][$sex][$level] ?? 0;

        if ($config_num == -1) return -1; //配置-1 每次都返回有1次可看

        $current_time = time();
        $day_s_time = strtotime(date("Y-m-d"), $current_time);
        $day_e_time = $day_s_time + 86399;
        $where = [
            ['member_id', 'eq', $member_id],
            ['view_time', 'between', $day_s_time . ',' . $day_e_time],
            ['type', 'eq', $type],
        ];


        $viewLogModel = model('common/view/ViewLog');
        $issued_num = $viewLogModel->getCount($where);//统计总查看次数
        $surplus_num = $config_num - $issued_num;

        return $surplus_num > 0 ? $surplus_num : 0;
    }


    /**
     * User: Simony
     * Function: 获取过期配置
     * @param $member_id
     * @param $config_num_name
     * @return array|float|int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function getExpireConfig($member_id, $config_num_name)
    {

        $memberModel = model('common/member/Member');

        $member = $memberModel->getInfoByID($member_id);

        if (empty($member)) return ds_arr(ERROR, '会员不存在');

        //读取配置动态数 level_auth[$config_num_name][性别即1][等级]
        $level_auth = moduleConfig('level_auth');
        $sex = $member['sex'];
        $level = $member['level'];
        $config_day = $level_auth[$config_num_name][$sex][$level] ?? 0;
        return $config_day;
    }


    /**
     * User: Simony
     * Function: 查看是否已过期 过期true
     * @param $member_id
     * @param $been_member_id
     * @param $type 1，会员信息 2微信+聊天社交 3，相册
     * @return array|bool
     */
    public function isExpire($member_id, $been_member_id, $type = 1)
    {
        if (!in_array($type, [1, 2, 3])) return ds_arr(ERROR, '类型必须：1，会员信息 2微信+聊天社交 3，相册');
        if ($member_id == $been_member_id) return false;

        $memberModel = model('common/member/Member');

        $member = $memberModel->getInfoByID($member_id);

        if (empty($member)) return ds_arr(ERROR, '会员不存在');

        $viewLogModel = model('common/view/ViewLog');
        $current_time = time();
        $where = [
            ['member_id', 'eq', $member_id],
            ['been_member_id', 'eq', $been_member_id],
            ['expire_time', 'gt', $current_time],  //有效期>当前时间 ，说明有效
            ['type', 'eq', $type]
        ];
        $num = $viewLogModel->getCount($where);
        return $num <= 0 ? true : false;

    }


}
